﻿//using SaaS.Core;
//using SaaS.Model.Entity.Auths;
//using SaaS.Model.Enums;
//using SaaS.Model.Dto.Auths;
//using Web.System.Services.Utils;
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Linq;
//using System.Text;
//using XCore.Dapper;
//using XCore.Logger;
//using XCore.Dependency;

//namespace Web.System.Services.Auths
//{
//    public class MenuApplicationService : SystemApplicationServiceBase, ITransientDependency
//    {
//        public bool Authoriza(int id, string[] permissions)
//        {
//            if (!SDC.Menus.Any(c => c.Id == id)) throw new MessageException(CoreSetting.Error_ID, (int)LogSource.Peration);
//            var list = PermissionFinder.GetPermissions(id, permissions, PermissionType.Menu);
//            if (list.Count() > 0)
//            {
//                using (IDapperDatabase dapper = CreateDapperDatabase(IsolationLevel.ReadUncommitted))
//                {
//                    dapper.Delete<Permission>(c => c.MenuId == id);
//                    dapper.Insert<Permission, int>(list);
//                    dapper.Commit();
//                }
//                return true;
//            }
//            else
//            {
//                return Repository.Delete<Permission>(c => c.MenuId == id);
//            }
//        }

//        public IEnumerable<MenuChildren> GetMenus(IEnumerable<string> permissions)
//        {
//            var menus = SDC.Permissions.Where(c => c.MenuId.HasValue).Where(c => permissions.Contains(c.Name)).Select(c => c.MenuId.Value).ToArray();
//            var query = SDC.Menus.Where(c => c.IsVisble).Where(c => menus.Contains(c.Id)).OrderByDescending(c => c.Sort).Select(c => new MenuDto
//            {
//                AlwaysShow = c.AlwaysShow,
//                Name = c.Name,
//                Component = c.Component,
//                Icon = c.Icon,
//                Id = c.Id,
//                ParentId = c.ParentId,
//                Path = c.Path,
//                Redirect = c.Redirect,
//                Title = c.Title
//            });
//            return MenuFinder.GetMenuChlden(query);
//        }

//        public bool Sort(int id, int value)
//        {
//            using (IDapperDatabase dapper = CreateDapperDatabase())
//            {
//                return dapper.Execute($"update auth_menus set sort+={value} where id={id}");
//            }
//        }

//        public bool Visble(int id)
//        {
//            using (IDapperDatabase dapper = CreateDapperDatabase())
//            {
//                return dapper.Execute("update auth_menus set isvisble=~isvisble where id=" + id);
//            }
//        }

//        public IEnumerable<string> GetPermissions(int id)
//        {
//            return SDC.Permissions.Where(c => c.MenuId == id).Select(c => c.Name);
//        }
//    }
//}
